package io.test;

import java.io.*;
import java.util.Arrays;

public class GetObjFile {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //获取当前目录
        File dir = new File("src/io/test");
        if (dir.isDirectory()) {
            //获取该路径下所有以".obj"结尾的子项
            File[] subs = dir.listFiles(f -> f.getName().endsWith(".obj"));
            for (int i = 0; i < subs.length; i++) {//循环符合条件的每一项
                //字节输入流,连接文件和程序,获取文件的内容
                FileInputStream fis = new FileInputStream("src/io/test/"+subs[i].getName());
                ObjectInputStream ois = new ObjectInputStream(fis);
                Object obj = ois.readObject();//从对象输入流获取对象(反序列化)
                if (obj instanceof User){//判断对象的实例是否是User的引用
                    User user = (User) obj;
                    System.out.println(user);
                }
            }
        }
    }

    //test:获取所有文件(file-test-GetFile)
    public File[] getFile(File dir){
        File[] subs = {};
        if (dir.isDirectory()){
            /*fillter*/
            File[]  sub = dir.listFiles(f -> f.getName().endsWith("obj"));
            subs = Arrays.copyOf(subs,subs.length+sub.length);
            System.arraycopy(sub,0,subs,subs.length - sub.length,sub.length);
        }else if (dir.getName().endsWith("obj")){
            subs = Arrays.copyOf(subs,subs.length+1);
            subs[subs.length - 1] = dir;
        }
        return subs;
    }
}
